var System = {
	init:function(){
		System.validationMessages = jQuery('div#validationMessages').dialog({
			show:"drop",title:"Validation!",autoOpen:false,height:100,width:170,
			stack:false
		});
		System.systemMessages = jQuery('div#systemMessages').dialog({
			show:"drop",title:"Notification",autoOpen:false,height:100,width:170,
			stack:false
		});
		System.expandedFund = -1;
	},
	
	showValidation: function(message,left){
		var jFocused = jQuery("*:focus");
		System.validationMessages.dialog('close');
		var vert = $(window).height() / 2;
		if(jFocused.length == 1){
			var scrollTop = jQuery(document).scrollTop();
			var offset = jFocused.offset().top;
			vert = offset - scrollTop - 40;
		}
		var showLeft = left || false;
		System.validationMessages.empty();
		System.validationMessages.html("<span class='validation'>"+message+"</span>");
		var width = ($(window).width() / 2);
		if(showLeft){
			width -= 626;
			System.validationMessages.dialog("option","show",{effect:"drop",direction:"right"});
		} else {
			width += 420;
			System.validationMessages.dialog("option","show",{effect:"drop",direction:"left"});
		}
		System.validationMessages.dialog("option","position",[width,vert]);
		System.validationMessages.dialog('open');
		jFocused.focus();
	},
	
	showMessage: function(message,left){
		System.systemMessages.dialog('close');
		var showLeft = left || false;
		System.systemMessages.empty();
		System.systemMessages.html("<span class='instructions'>"+message+"</span>");
		var width = ($(window).width() / 2);
		if(showLeft){
			width -= 596;
			System.systemMessages.dialog("option","show",{effect:"drop",direction:"right"});
		} else {
			width += 420;
			System.systemMessages.dialog("option","show",{effect:"drop",direction:"left"});
		}
		var vert = ($(window).height() / 2) - 120;
		System.systemMessages.dialog("option","position",[width,vert]);
		System.systemMessages.dialog('open');
	},
	
	isUser: function(){
		var userID = +jQuery('input#userID').val() || -1;
		if(userID > 0){
			return true;
		} else {
			System.validationMessage("Please Log in or create an account.")
			return false;
		}
	},
	
	toggleFundContents: function(fundID) {
		var bar_content = jQuery('div#meter_wrap_'+fundID).find('div.bar_content');
		if(!bar_content.is(':visible')){
			bar_content.slideDown();
			if(System.expandedFund > 0){
				System.reloadFundRow(System.expandedFund, 'donate');
			}
			System.expandedFund = fundID;
		}
	},
	
	reloadFundRow: function(fundID,action){
		jQuery.ajax({
			url:"/cfcs/remote.cfc?cfc=fundUtil&method=run&callMethod=renderFundRow&action="+action+"&fundID="+fundID,
			type:"GET",
			success:function(data){
				var row = jQuery('div#meter_wrap_'+fundID);
				var content = row.find('div.bar_content');
				if(content.is(":visible")){
					content.slideUp(function(){
						row.replaceWith(data);
					});
				} else {
					row.replaceWith(data);
				}
			}
		});
	}
};

jQuery(document).ready(function(){
	System.init();
});